<!doctype html>
<html>
    <head>
        <title>AudioTrackList &amp; VideoTrackList addtrack event</title>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
            </head>
    <body>
        <script>
            function addtrack_test(tagName, src, label)
            {
                function onTrackListEvent(actualEventList, e)
                {
                    actualEventList.push(e.type);

                    if (e.type == "addtrack") {
                        assert_equals(e.track, e.target[0], "Track property matches first track in list.");
                    }
                }

                function setupTrackListHandlers(t, trackList, actualEventList)
                {
                    trackList.addEventListener("addtrack", t.step_func(onTrackListEvent.bind(this, actualEventList)));
                    trackList.addEventListener("change", t.step_func(onTrackListEvent.bind(this, actualEventList)));
                    trackList.addEventListener("removetrack", t.step_func(onTrackListEvent.bind(this, actualEventList)));
                }

                async_test(function(t)
                {
                    var media = document.createElement(tagName);
                    media.src = src;

                    var expectedEvents = ["addtrack"];
                    var actualAudioEvents = [];
                    var actualVideoEvents = [];
                    setupTrackListHandlers(t, media.audioTracks, actualAudioEvents, "audioTracks events");
                    setupTrackListHandlers(t, media.videoTracks, actualVideoEvents, "videoTracks events");

                    media.load();

                    media.addEventListener("loadedmetadata", t.step_func_done(function()
                    {
                        assert_array_equals(actualAudioEvents, expectedEvents);

                        if (media.videoTracks.length > 0) {
                            assert_equals(label, "audio-video");
                            assert_array_equals(actualVideoEvents, expectedEvents);
                        } else {
                            assert_equals(label, "audio-only");
                            assert_equals(actualVideoEvents.length, 0);
                        }
                    }));
                }, tagName + " : " + label);
            }

            addtrack_test("audio", "../content/test.oga", "audio-only");
            addtrack_test("audio", "../content/test.ogv", "audio-video");
            addtrack_test("video", "../content/test.oga", "audio-only");
            addtrack_test("video", "../content/test.ogv", "audio-video");
        </script>
    </body>
</html>
